package com.caipiao.crawler.daletou;

import com.alibaba.fastjson.JSON;
import com.caipiao.crawler.mapper.LotteryCurrentMapper;
import com.caipiao.crawler.mapper.LotteryNumberMapper;
import com.caipiao.crawler.mapper.LotteryTicketMapper;
import com.caipiao.crawler.model.LotteryNumber;
import com.caipiao.crawler.model.LotteryTicket;
import com.caipiao.crawler.zhongjiangRule.ZhongJiangRule;
import com.caipiao.util.DBTools;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.ibatis.session.SqlSession;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by adminitrator on 2018/7/28.
 */
public class DaletouResultPipeline implements Pipeline {

    private static Log log = LogFactory.getLog(DaletouResultPipeline.class);

    @Override
    public synchronized void process(ResultItems resultItems, Task task) {
        LotteryNumber m = resultItems.get("model");
      log.info("pipe process:" + Thread.currentThread().getId());
        if(m == null) {
            return;
        }
        commit(m);
    }

    public static boolean checkElement(String[] arr,String targetValue){
        return Arrays.asList(arr).contains(targetValue);
    }

    public void commit(LotteryNumber model) {
//        log.info("--------大乐透数据入库开始---------");
//        //TODO 写数据库，需要判断不存在的情况才写入
//        SqlSession session = DBTools.getSession();
//        LotteryNumberMapper mapper = session.getMapper(LotteryNumberMapper.class);
//        LotteryTicketMapper ticketMapper = session.getMapper(LotteryTicketMapper.class);
//        LotteryCurrentMapper currentMapper = session.getMapper(LotteryCurrentMapper.class);
//       Map map1 = new HashMap();
//       map1.put("term",model.getTerm());
//       map1.put("type","0");
//        int count = mapper.selectCountByTerm(map1);
//        map1.clear();
//        map1.put("type","0");
//        String lastTerm = mapper.selectTermByTypeAndTerm(map1);
//        map1.put("term",Integer.parseInt(lastTerm));
//        map1.put("type","0");
//     List<LotteryNumber> modelList =  mapper.selectModelByTermAndType(map1);
//        LotteryNumber lastModel = modelList.get(0);
//        String lastNum = lastModel.getNumber();
//        String[] lastArr = lastNum.split("-");
//        String[] qianquLast = lastArr[0].split(" ");
//        String[] houquLast = lastArr[1].split(" ");
//        String before = lastModel.getBefore_area();
//        String after = lastModel.getAfter_area();
//        String[] beforeArr = before.split(" ");
//        String[] afterArr = after.split(" ");
//        String number1 = model.getNumber();
//        String[] prizeNumberArray = number1.split("-");
//        String[] qianquArray = prizeNumberArray[0].split(" ");
//        String[] houquArray = prizeNumberArray[1].split(" ");
//        for(int i=0;i<beforeArr.length;i++){
//            if((Integer.parseInt(beforeArr[i])==i+1)&&checkElement(qianquLast,beforeArr[i])){
//                beforeArr[i] = "1";
//            }else{
//                beforeArr[i] = String.valueOf((Integer.parseInt(beforeArr[i])+1));
//            }
//        }
//        for(int i=0;i<afterArr.length;i++){
//            if((Integer.parseInt(afterArr[i])==i+1)&&checkElement(houquLast,afterArr[i])){
//                afterArr[i] = "1";
//            }else{
//                afterArr[i] = String.valueOf((Integer.parseInt(afterArr[i])+1));
//            }
//        }
//        for(int i=0;i<qianquArray.length;i++){
//            beforeArr[Integer.parseInt(qianquArray[i])-1]=qianquArray[i];
//        }
//        for(int i=0;i<houquArray.length;i++){
//            afterArr[Integer.parseInt(houquArray[i])-1]=houquArray[i];
//        }
//        String beforeStr = Arrays.toString(beforeArr).replaceAll(",","");
//        beforeStr = beforeStr.substring(1,beforeStr.length()-1);
//        String afterStr = Arrays.toString(afterArr).replaceAll(",","");
//        afterStr = afterStr.substring(1,afterStr.length()-1);
//        model.setBefore_area(beforeStr);
//        model.setAfter_area(afterStr);
//        if(count==0) {
//            mapper.insertModel(model);
//            map1.clear();
//            map1.put("term",model.getTerm());
//            map1.put("name","daletou");
//            map1.put("updated_time",(int) (System.currentTimeMillis()/1000L));
//            currentMapper.updateCurrent(map1);
//        }else{
//            map1.clear();
//            map1.put("term",model.getTerm());
//            map1.put("type","0");
//            map1.put("before_area",beforeStr);
//            map1.put("after_area",afterStr);
//            mapper.updateChartForDlt(map1);
//        }
//        session.commit();
//        log.info("--------大乐透数据入库结束---------");
//        //TODO 如果第一次写入，触发更新判断是否中奖的逻辑
//        log.info("--------大乐透数据中奖开始---------");
//        if(count==0){
//            String prizeNumber = model.getNumber(); //中奖号码
//            Map map = new HashMap<String,Object>();
//            map.put("term",model.getTerm());
//            map.put("type","1");
//            map.put("status","1");
//           List<LotteryTicket>  ticketList = ticketMapper.selectByTermAndType(map);
//           for(LotteryTicket lotteryTicket : ticketList){
//               String number = lotteryTicket.getNumber();
//               Map ticketMap = ZhongJiangRule.daletouRule(number,model.getNumber(),model,lotteryTicket.getMultiple());
//               ticketMap.put("id",lotteryTicket.getId());
//               ticketMap.put("updated_time",(int) (System.currentTimeMillis()/1000L));
//               ticketMapper.updateTicketById(ticketMap);
//           }
//           session.commit();
//           session.close();
//        }
//        log.info("--------大乐透数据中奖结束---------");
    }

}
